package com.cn.yugan.Engine.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.yugan.Engine.Beans.ArticleListInfo;
import com.cn.yugan.Engine.Beans.UserInfo;
import com.cn.yugan.Engine.Holder.MarkItemHolder;
import com.cn.yugan.mobile.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Rover on 2016/7/22 .
 * Email 357319517@qq.com
 */
public class MarkListViewAdpater extends BaseAdapter {
    private Context context;
    private ArrayList<UserInfo> mlist;
    private LayoutInflater layoutInflater;
    private MarkItemHolder markItemHolder;
    private int status;

    public MarkListViewAdpater(Context context, ArrayList<UserInfo> mlist) {
        this.context = context;
        this.mlist = mlist;
        this.layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        markItemHolder = null;

        if (convertView == null) {
            markItemHolder = new MarkItemHolder();
            convertView = layoutInflater.inflate(R.layout.list_item_mark, null);
            markItemHolder.userimage = (ImageView) convertView.findViewById(R.id.markitem_cover_user_photo);
            markItemHolder.name = (TextView) convertView.findViewById(R.id.markitem_name_tv);
            markItemHolder.intro = (TextView) convertView.findViewById(R.id.markitem_intro_tv);
            convertView.setTag(markItemHolder);
        } else {
            markItemHolder = (MarkItemHolder) convertView.getTag();
        }
        if (mlist.get(position).getImg() == null||mlist.get(position).getImg().isEmpty()||mlist.get(position).getImg().equals("")) {
            Picasso.with(context).load(R.mipmap.default_img).into(markItemHolder.userimage);
        }else {
            Picasso.with(context).load(context.getString(R.string.imgserverurl)+mlist.get(position).getImg().replace("?", "!userface75?")).error(R.mipmap.default_img).into(markItemHolder.userimage);
        }
        markItemHolder.name.setText("" + mlist.get(position).getUsername());
        markItemHolder.intro.setText("" + mlist.get(position).getIntro());

        return convertView;
    }

    public void refreshList(ArrayList<UserInfo> mlist) {
        if (mlist == null) {

        } else {
            this.mlist = mlist;
        }

    }

    public void addList(ArrayList<UserInfo> mlist) {
        if (mlist == null) {

        } else {
            this.mlist.addAll(mlist);
        }

    }
}
